package Day2025_03_16;

import java.util.HashMap;
import java.util.Map;

public class LeetCode873 {
    public int lenLongestFibSubseq(int[] arr) {
        int n = arr.length;
        Map<Integer, Integer> hash = new HashMap<>();
        int[][] dp = new int[n][n];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                dp[i][j] = 2;
        for (int i = 0; i < n; i++) hash.put(arr[i], i);
        int ret = 2;
        for (int i = 2; i < n; i++) {
            for (int j = i - 1; j >= 1; j--) {
                int tmp = arr[i] - arr[j];
                if (tmp < arr[j] && hash.containsKey(tmp)) {
                    dp[j][i] = dp[hash.get(tmp)][j] + 1;
                }
                ret = Math.max(ret, dp[j][i]);
            }
        }
        return ret == 2 ? 0 : ret;
    }
}
